IF OBJECT_ID('ssf.ptx_get_task_group_execution_log') IS NOT NULL
	DROP FUNCTION ssf.ptx_get_task_group_execution_log
GO

CREATE FUNCTION ssf.ptx_get_task_group_execution_log (
	@task_group_id INT = NULL
)
RETURNS TABLE
AS
/*
<object_header>

<object_summary>
	<purpose><![CDATA[

	]]></purpose>
</object_summary>

</object_header>
*/

	RETURN (
		WITH
		task_log AS (
			SELECT DISTINCT ssf.ptx_parse_task_description_task_group_run_id(l.task_description) AS task_group_run_id
			FROM ssf.log_info_all l
			CROSS APPLY ssf.ptx_parse_task_job_name(l.target_object) j
			WHERE l.source_object = 'ssf.ptx_run_task_group'
			AND j.task_group_id = ISNULL(@task_group_id, j.task_group_id)
		)
		SELECT r.group_type
			, r.group_name
			, r.task_group_run_date
			, j.task_group_id
			, j.task_id
			, l.*
		FROM ssf.log_info_all l
		CROSS APPLY ssf.ptx_parse_task_group_run_id(
			ssf.ptx_parse_task_description_task_group_run_id(l.task_description)
		) r
		OUTER APPLY ssf.ptx_parse_task_job_name(l.target_object) j
		WHERE ssf.ptx_parse_task_description_task_group_run_id(l.task_description) IN (
			SELECT task_group_run_id FROM task_log
		)
	)
GO
